package cn.itcast.order.service.impl;

import cn.itcast.feign.clients.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderSericeImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserClient userClient;
    @Override
    public Order queryOrderById(Integer orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.发起Feign远程调用
        User user = userClient.findById(order.getUserId());
        // 3.封装user到order
        order.setUser(user);
        // 4.返回
        return order;
    }
//    @Autowired
//    private RestTemplate restTemplate;
//    @Override
//    public Order queryOrderById(Integer orderId) {
//        // 查询订单
//        Order order = orderMapper.findById(orderId);
//        // 利用 RestTemplate 发起http请求 查询用户
//        // url 路径
////        String url = "http://localhost:8010/user/" + order.getUserId();
//        String url = "http://userservice/user/" + order.getUserId();
//        // 发起http请求 实现远程调用
//        User user = restTemplate.getForObject(url, User.class);
//        order.setUser(user);
//        return order;
//    }
}
